/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header$
 */
package cn.newtest.bi.collection.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;

/**
 * 模版内区域定义
 *
 * @author <a href="mailto: chenjian@risesoft.net">陈健</a>
 * @version $Revision$
 * 
 */
@Entity
@org.hibernate.annotations.Entity(dynamicInsert=true,dynamicUpdate=true)
@Table(name="Rpt_Template_Range")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class TemplateRange implements Comparable<Object>, Serializable {
	
	private static final long serialVersionUID = 8641373057107870696L;
	
	public static final int RANGETYPE_NOTEXPANDED = 1;
	public static final int RANGETYPE_ERWEIZENGYI = 2;
	public static final int RANGETYPE_TOPTOBOTTOM = 3;
	public static final int RANGETYPE_LEFTTORIGHT = 4;
	public static final int RANGETYPE_BIDIRECTION = 5;

	@Id
	@Column(name="TR_GUID",length=38)
	@GenericGenerator(name="TemplateRange-assigned",strategy="assigned")
	private String guid;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="Template_GUID")
	private Template template;
	
	@Column(name="Range_Name",length=20)
	private String name;
	
	/**
	 * range类型：
	 *  1:不扩展
	 *  2:二维增一
	 *	3：仅从上向下扩展
	 *	4：仅从左向右扩展
	 *	5：从上向下且从左向右扩展
	 */
	@Column(name="Range_RangeType")
	private Integer rangeType;
	
	@Column(name="Range_IncludedAlias",length=500)
	private String includedAlias;
	
	@Column(name="Range_FixedRows")
	private Integer fixedRows;
	
	/**
	 * 假如模板中超过1个range，则将每个range中内含的catalogCode保存下来，用来回显用
	 */
	@Column(name="Range_ContainedCodes",length=500)
	private String containedCodes;
	
	@Basic
	private Double tabIndex;
	
	/**
	 * range所对应的catalogCode所在指标的列代码
	 */
	@Transient
	private String catalogCodeColumnCode;

	/**
	 * 将containedCodes转为list用来处理
	 */
	@Transient
	private List<String> containedCodeList;
	
	public Integer getFixedRows() {
		return fixedRows;
	}

	public void setFixedRows(Integer fixedRows) {
		this.fixedRows = fixedRows;
	}
	
	public String getContainedCodes() {
		return containedCodes;
	}

	public void setContainedCodes(String containedCodes) {
		this.containedCodes = containedCodes;
	}
	
	public Integer getRangeType() {
		return rangeType;
	}

	public void setRangeType(Integer rangeType) {
		this.rangeType = rangeType;
	}

	public String getGuid() {
		return guid;
	}

	public void setGuid(String guid) {
		this.guid = guid;
	}

	public String getIncludedAlias() {
		return includedAlias;
	}

	public void setIncludedAlias(String includedAlias) {
		this.includedAlias = includedAlias;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Double getTabIndex() {
		return tabIndex;
	}

	public void setTabIndex(Double tabIndex) {
		this.tabIndex = tabIndex;
	}

	public Template getTemplate() {
		return template;
	}

	public void setTemplate(Template template) {
		this.template = template;
	}
	
	public List<String> getContainedCodeList() {
		if(containedCodeList==null){
			if(containedCodes==null||containedCodes.equals(""))
				return null;
			String[] containedCodeColl = containedCodes.split(",");
			containedCodeList = new ArrayList<String>();
			for(int i=0;i<containedCodeColl.length;i++){
				containedCodeList.add(containedCodeColl[i]);
			}
		}
		return containedCodeList;
	}

	public void setContainedCodeList(List<String> containedCodeList) {
		this.containedCodeList = containedCodeList;
	}
	
	public String getCatalogCodeColumnCode() {
		return catalogCodeColumnCode;
	}

	public void setCatalogCodeColumnCode(String catalogCodeColumnCode) {
		this.catalogCodeColumnCode = catalogCodeColumnCode;
	}

	/**
	 * @see java.lang.Comparable#compareTo(Object)
	 */
	public int compareTo(Object object) {
		TemplateRange myClass = (TemplateRange) object;
		return new CompareToBuilder().append(this.tabIndex, myClass.tabIndex).append(this.rangeType, myClass.rangeType).append(this.includedAlias, myClass.includedAlias).append(this.guid, myClass.guid).append(this.name, myClass.name).toComparison();
	}

	/**
	 * @see java.lang.Object#equals(Object)
	 */
	public boolean equals(Object object) {
		if (!(object instanceof TemplateRange)) {
			return false;
		}
		TemplateRange rhs = (TemplateRange) object;
		return new EqualsBuilder().appendSuper(super.equals(object)).append(this.tabIndex, rhs.tabIndex).append(this.rangeType, rhs.rangeType).append(this.includedAlias, rhs.includedAlias).append(this.guid, rhs.guid).append(this.name, rhs.name).isEquals();
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(-1499208159, -734037377).appendSuper(super.hashCode()).append(this.tabIndex).append(this.rangeType).append(this.includedAlias).append(this.guid).append(this.name).toHashCode();
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this).append("tabindex", this.tabIndex).append("name", this.name).append("includedAlias", this.includedAlias).append("guid", this.guid).append("expandDirection", this.rangeType).toString();
	}
}